﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Manage_GoodsAdd_CompanySearch_dl2 : System.Web.UI.Page
{
    string str_Goodsid = "", AccountName_sc, Projectid, username_sc;
    Int64 accountidsc;
    Int64 accountid_tb=0;
    bool haveBidded = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
         if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))  //是否为合法用户
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            accountidsc = _Account.AccountId;
            AccountName_sc = _Account.AccountName;
            username_sc = _Account.DWName;
            Projectid = Session["Projectid"].ToString();
            str_Goodsid =Request.QueryString["Goodsid"].Trim();
            if(string.IsNullOrEmpty(Request.QueryString["Accountid"].Trim())==false)
            {
                accountid_tb = Convert.ToInt64(Request.QueryString["Accountid"].Trim());
            }

            string sqlstr = " select count(1) from Project_Bid where goodsid=" + str_Goodsid  ;
            Db db = new Db();
            DataTable gg = db.DataTable(sqlstr);
            if (gg != null && gg.Rows[0][0].Equals(0) == false)
            {
                haveBidded = true;//已经投标
            }

          
              sqlstr = " select count(1) from goods where goodsid=" + str_Goodsid + "and accountid_tjr="+accountidsc;
             
              gg = db.DataTable(sqlstr);
            if (gg != null && gg.Rows[0][0].Equals(1) == false)
            {
                haveBidded = true;//已经投标
                Response.Write("<script>alert('非法操作!您无权设置此商品的投标人！');window.opener.location.reload();window.close();</script>");
            }

            db.Close();
            gg.Dispose(); 

            showGridView();

            
        }
    }
    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        string strConditions;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Company a inner join ProjectCompanyAttribution b on a.accountid = b.accountid";                                               //要查询的表明
        dbPage.Key = "a.accountid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "a.CompanyName,a.usercode,a.accountid";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 25;                                                    //没有显示记录条数   
        dbPage.OrderBy = "CompanyName";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp , CompanyName = "", usercode = "";
        usercode = txtUserCode.Text.ToString().Trim();
        CompanyName = txtAttorneyName.Text.ToString().Trim();
        strtemp = "a.checkstatus=1 and b.projectid="+Projectid+ "and (a.sort=1 or a.accountid=" + accountidsc.ToString() + ") and";
        //审核通过的经营企业,是中标项目
        if (usercode != "")
        {
            strtemp = strtemp + " a.usercode like '%" + usercode + "%' and";
        }
        if (CompanyName != "")
        {
            strtemp = strtemp + " a.CompanyName like '%" + CompanyName + "%' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.CompareTo("Update") == 0)
        {
            accountid_tb = Convert.ToInt64(e.CommandArgument.ToString().Trim());
            int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
      
            string str_sql = "";
        //每个生产企业的同一个通用名的商品只能给一家投标人投标或者给自己投标
            string sql = " select count(*) from bid_goods_tbr where goodsid = '" + str_Goodsid +  "' and projectid=" + Projectid;
        Db gg = new Db();
        DataTable panduan = gg.DataTable(sql);
        int _count = Convert.ToInt32(panduan.Rows[0][0].ToString().Trim());
        if (_count > 0)//如果有则更新,没有则插入
        {
            str_sql = " update bid_goods_tbr set status=0, accountid_tb=" + accountid_tb + ",usercode_TB ='" + GridView1.Rows[rowIndex].Cells[2].Text + "', username_tb='" + GridView1.Rows[rowIndex].Cells[1].Text + "',edit_time =getdate(),Accept_time=null where goodsid=" + str_Goodsid + " and projectid=" + Projectid;
        }
        else
        {

            str_sql = " insert into bid_goods_tbr (projectid,goodsid,accountid_sc,accountid_tb,username_tb,usercode_tb) values(" + Projectid + ",'" + str_Goodsid + "',"  + accountidsc+"," + accountid_tb + ",'" + GridView1.Rows[rowIndex].Cells[1].Text + "','" + GridView1.Rows[rowIndex].Cells[2].Text + "')";
        }
        if (gg.Command(str_sql) == 1)
        {
            Response.Write("<script>alert('投标人设置成功！');window.opener.location.reload();window.close();</script>");
        }
        else
        {
            Response.Write("<script>alert('投标人设置失败！');window.opener.location.reload();window.close();</script>");
        }
        gg.Close();
        panduan.Dispose();
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    
   
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
        }

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
            if (e.Row.Cells[2].Text == AccountName_sc)//生产企业本身以黄色显示
            {
                e.Row.BackColor = System.Drawing.Color.Yellow;
            }
            if (e.Row.Cells[0].Text == accountid_tb.ToString())// 投标人
            {
                e.Row.BackColor = System.Drawing.Color.Green;
            }

        }
        if (haveBidded)
       {
           e.Row.Cells[3].Enabled = false;
       }
        
    }
}
